package com.ccp.dev.form.service.bus;

import com.ccp.dev.core.basic.engine.GroovyScriptEngine;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.JSONObjectUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.form.util.CommonVar;
import com.ccp.dev.system.model.Position;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysRole;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 对象功能:查询帮助类
 * 	开发人员:zxh
 * 	创建时间:2013-10-16 16:38:00
 * </pre>
 */
public class QueryUtil {
    private QueryUtil() {
    }

    /**
     * 判断是否有权限。
     *
     * @param permission
     * @param rightMap   权限map
     * @return
     */
    public static boolean hasRight(JSONObject permission, Map<String, Object> rightMap) {
        String type = permission.get("type").toString();
        Object script = permission.get("script");
        // 无
        if ("none".equals(type)) {
            return false;
        }
        // 所有人
        if ("everyone".equals(type)) {
            return true;
        }
        String id = "";
        if (permission.containsKey("ids")) {
            id = permission.get("ids").toString();
        }
        String[] userId = null;
        String[] curOrgId = null;
        if (BeanUtils.isNotEmpty(rightMap.get("userId"))) {
            userId = rightMap.get("userId").toString().split(",");
        }
        if (BeanUtils.isNotEmpty(rightMap.get("curOrgId"))) {
            curOrgId = rightMap.get("curOrgId").toString().split(",");
        }

        List<SysRole> roles = (List<SysRole>) rightMap.get("roles");
        List<Position> positions = (List<Position>) rightMap.get("positions");
        SysOrg orgs = (SysOrg) rightMap.get("orgs");
        // 指定用户
        if ("user".equals(type)) {
            if (BeanUtils.isNotEmpty(userId)) {
                for (String ui : userId) {
                    return StringUtil.contain(id, ui);
                }
            }
        }
        // 指定角色
        else if ("role".equals(type)) {
            if (BeanUtils.isNotEmpty(roles)) {
                for (SysRole role : roles) {
                    if (StringUtil.contain(id, role.getRoleId())) {
                        return true;
                    }
                }
            }
        }
        // 指定组织
//        else if ("org".equals(type)) {
//            if (BeanUtils.isNotEmpty(orgs)) {
//                for (SysOrg org : orgs) {
//                    if (StringUtil.contain(id, org.getOrgId())) {
//                        return true;
//                    }
//                }
//            }
//        }
        else if ("org".equals(type)) {
            if (BeanUtils.isNotEmpty(orgs)) {
                if (StringUtil.contain(id, orgs.getOrgId())) {
                    return true;
                }
            }
        }
        // 岗位
        else if ("pos".equals(type)) {
            if (BeanUtils.isNotEmpty(positions)) {
                for (Position position : positions) {
                    if (StringUtil.contain(id, position.getPosId())) {
                        return true;
                    }
                }
            }
        } else if ("script".equals(type)) {
            if (BeanUtils.isEmpty(script)) {
                return false;
            }
            Map<String, Object> map = new HashMap<>(16);
            if (userId != null && curOrgId != null) {
                CommonVar.setCurrentVars(map);
            }
            GroovyScriptEngine groovyScriptEngine = AppUtil.getBean(GroovyScriptEngine.class);
            return groovyScriptEngine.executeBoolean(script.toString(), map);
        }
        return false;
    }

    /**
     * 获取权限map
     *
     * @param type
     * @param jsonAry
     * @param rightMap
     * @return
     */
    public static Map<String, Boolean> getPermissionMap(int type, JSONArray jsonAry, Map<String, Object> rightMap) {
        Map<String, Boolean> map = new HashMap<>(16);
        if (JSONObjectUtil.isEmpty(jsonAry)) {
            return map;
        }

        for (Object obj : jsonAry) {
            JSONObject json = JSONObject.fromObject(obj);
            String var = QueryUtil.getRightsName(json);
            JSONArray rights = (JSONArray) json.get("right");
            Iterator<JSONObject> iterator = rights.iterator();
            boolean hasRight = false;
            while (true) {
                JSONObject permission = iterator.next();
                Integer s = (Integer) permission.get("s");
                if (s == null || s.intValue() == type && permission.containsKey("type")) {
                    hasRight = hasRight(permission, rightMap);
                    if (hasRight) {
                        map.put(var, hasRight);
                        break;
                    }
                }
                if (!iterator.hasNext()) {
                    map.put(var, hasRight);
                    break;
                }
            }
        }
        return map;
    }

    /**
     * 获取权限名称
     *
     * @param json
     * @return
     */
    public static String getRightsName(JSONObject json) {
        String var = (String) json.get("variable");
        if (StringUtil.isEmpty(var)) {
            var = json.getString("name");
        }
        return var;
    }
}
